<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

class LettermanModelCampaign extends JModel
{
     
     var $_data = null;
    
     var $id = null;

    function __construct()
    {
            parent::__construct();
    
            $array = JRequest::getVar('cid', array(0), '', 'array');
            $edit = JRequest::getVar('edit',true);
            if($edit)
                    $this->setId((int)$array[0]);
    }
        
        function setId($id)
	{
		$this->_id = $id;
		$this->_data = null;
	}
        
	function &getData()
	{
		if (empty($this->_data))
		{
                    $query =  "SELECT * " .
                            "FROM #__letterman_campaigns " .
                            "WHERE id = '$this->_id'";
                    $this->_db->setQuery($query);
                    $this->_data = $this->_db->loadObject();
                }
                
		return $this->_data;
	}        
        
	function store($data)
	{
		global $mainframe;

		$database =& JFactory::getDBO();

		$row	=& JTable::getInstance('Campaigns', 'Table');
		if (!$row->bind( $data ))
		{
			JError::raiseError(500, $row->getError() );
		}

		// Convert British dd/mm/yyy to mySQL yyyy-dd-mm
		if (ereg ("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4})", $row->apps_close, $regs)) {
	   		$row->apps_close = "$regs[3]-$regs[2]-$regs[1] 00:00:00";
		}

		$row->description = JRequest::getVar( 'description', '', 'post', 'string', JREQUEST_ALLOWRAW );

		if ($row->id) {
			$row->modified_time = date( "Y-m-d H:i:s" );
			$row->modified_by = $my->id;
		} else {
			$row->created_time = date( "Y-m-d H:i:s" );
			$row->created_by = $my->id;
		}

		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return true;
	}        
        
    function delete($id)
	{
			$sql = "DELETE FROM #__letterman_campaigns  " .
				"WHERE id = $id";
			$this->_db->setQuery( $sql );
			if(!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
			return true ;
	}
        
	function publish($cid = array(), $publish )
	{

		if (count( $cid ))
		{
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );

			$query = 'UPDATE #__letterman_campaigns'
				. ' SET published = '.(int) $publish
				. ' WHERE id IN ( '.$cids.' )'
			;
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}        

}
?>